/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.mall.biz.freeze.service.impl;


import co.yixiang.common.core.utils.FileUtil;
import co.yixiang.common.mproot.common.service.impl.BaseServiceImpl;
import co.yixiang.common.mproot.common.utils.QueryHelpPlus;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.mall.biz.freeze.mapper.TbUserRebateFreezeMapper;
import co.yixiang.mall.biz.freeze.service.TbUserRebateFreezeService;
import co.yixiang.mall.common.order.domain.TbUserRebateFreeze;
import co.yixiang.mall.common.order.dto.TbUserRebateFreezeDto;
import co.yixiang.mall.common.order.dto.TbUserRebateFreezeQueryCriteria;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author zhuhongjian
 * @date 2021-03-19
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "tbUserRebateFreeze")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TbUserRebateFreezeServiceImpl extends BaseServiceImpl<TbUserRebateFreezeMapper, TbUserRebateFreeze> implements TbUserRebateFreezeService {

    private final IGenerator generator;
    private final TbUserRebateFreezeMapper tbUserRebateFreezeMapper;

    @Override
    //@Cacheable
    public PageResult<TbUserRebateFreezeDto> queryAll(TbUserRebateFreezeQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TbUserRebateFreeze> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,TbUserRebateFreezeDto.class);
    }


    @Override
    //@Cacheable
    public List<TbUserRebateFreeze> queryAll(TbUserRebateFreezeQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TbUserRebateFreeze.class, criteria));
    }


    @Override
    public void download(List<TbUserRebateFreezeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbUserRebateFreezeDto tbUserRebateFreeze : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", tbUserRebateFreeze.getUserId());
            map.put("冻结金额", tbUserRebateFreeze.getMoney());
            map.put("冻结积分", tbUserRebateFreeze.getScore());
            map.put("冻结开始时间", tbUserRebateFreeze.getStartTime());
            map.put("冻结结束时间", tbUserRebateFreeze.getEndTime());
            map.put("冻结状态：1冻结中,2已发放,3已取消", tbUserRebateFreeze.getState());
            map.put("冻结累计金额(礼包商品)", tbUserRebateFreeze.getAccumulativeAmount());
            map.put("订单id", tbUserRebateFreeze.getOrderId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void unfreezeMoney() {
        tbUserRebateFreezeMapper.unfreezeMoney();
    }
}
